<?php

namespace app\modules\backend\model;

use app\models\tableModel\AdminUserModel;

class UserLoginModel extends AdminUserModel implements \yii\web\IdentityInterface
{

    /**
     * 用于区分前后台标识
     * @var bool
     */
    public $isAdmin = true;

    /**
     * {@inheritdoc}
     */
    public static function findIdentity($id)
    {

        $admin_ = self::findOne(['id' => $id]);

        if (!empty($admin_))
            return $admin_;

        return null;
    }

    /**
     * {@inheritdoc}
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        $admin_ = self::findOne(['accessToken' => $token]);

        if (!empty($admin_))
            return $admin_;

        return null;
    }

    /**
     * Finds user by username
     * @param $username
     * @return UserLoginModel|null
     */
    public static function findByUsername($username)
    {

        $admin_ = self::findOne(['user_name' => $username]);
        if (!empty($admin_)) return $admin_;

        return null;
    }

    /**
     *  Finds user by phone
     * @param $phone
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function findByPhone($phone)
    {

        $admin_ = self::findOne(['phone' => $phone]);
        if (!empty($admin_)) return $admin_;

        return null;
    }

    /**
     * {@inheritdoc}
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * {@inheritdoc}
     */
    public function getAuthKey()
    {
        return null;
    }

    /**
     * {@inheritdoc}
     */
    public function validateAuthKey($authKey)
    {
        return true;
    }

    /**
     * 获取角色编号
     * {@inheritdoc}
     */
    public function getRoleId()
    {
        return $this->role_id;
    }

    /**
     * Validates password
     * @param $password
     * @return bool
     */
    public function validatePassword($password)
    {

        $password = AdminUserModel::getPassCrypt($password, $this->salt);
        return $this->password_hash === $password;
    }
}
